import * as moment from "moment";
import { Schema, Prop, SchemaFactory } from "@nestjs/mongoose";
import { Document } from "mongoose";
import { Is } from "../constants/enum";


@Schema({ versionKey: false, timestamps: false })
export class BaseEntity extends Document {
  @Prop()
  created_at?: number;

  @Prop()
  created_by?: string;

  @Prop()
  updated_at?: number;

  @Prop()
  updated_by?: string;

  @Prop()
  deleted_at?: number;

  @Prop()
  deleted_by?: string;

  @Prop({ default: Is.no })
  is_deleted?: Is;
}

const BaseSchema = SchemaFactory.createForClass(BaseEntity);


BaseSchema.pre('save', function(next: any) {
  this.created_at = moment().unix();
  this.updated_at = moment().unix();
  next();
});

BaseSchema.methods.save = function(data: BaseEntity) {
  return this.create(data);
}

export { BaseSchema };